package com.hex.liyue.lookingdatemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.glide.GlideCircleTransform;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.SexUtils;
import com.hex.liyue.lookingdatemodule.bean.AppointmentDetailsResultBean;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterPerson extends RecyclerView.Adapter<AdapterPerson.MyHolder> {
    private AppointmentDetailsResultBean appointmentDetailsResultBean;
    private String[] namedArr;
    private OnItemClickListener onItemClickListener;

    public AdapterPerson(AppointmentDetailsResultBean appointmentDetailsResultBean) {
        this.appointmentDetailsResultBean = appointmentDetailsResultBean;
        if (appointmentDetailsResultBean.data.apply_list != null) {
            this.namedArr = new String[appointmentDetailsResultBean.data.apply_list.size()];
            for (int i = 0; i < namedArr.length; i++) {
                if (i == 0) {
                    namedArr[i] = "黄金骑士";
                } else if (i == 1) {
                    namedArr[i] = "银月骑士";
                } else if (i == 2) {
                    namedArr[i] = "青铜骑士";
                } else {
                    namedArr[i] = "普通骑士";
                }
            }
        }
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_person_details, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final AppointmentDetailsResultBean.DataBean.ApplyListBean applyListBean = appointmentDetailsResultBean.data.apply_list.get(position);
        L.e("applyListBean",applyListBean.toString());
        if ((position + 1) == 1) {
            holder.tv_top.setText(Html.fromHtml("<font color='#FED706'><big><big>1</big></big></font><font color='#FED706'>st</font>"));
        } else if ((position + 1) == 2) {
            holder.tv_top.setText(Html.fromHtml("<font color='#ff8c00'><big><big>2</big></big></font><font color='#ff8c00'>se</font>"));
        } else if ((position + 1) == 3) {
            holder.tv_top.setText(Html.fromHtml("<font color='#CBCBC2'><big><big>3</big></big></font><font color='#CBCBC2'>th</font>"));
        } else {
            int i=position+1;
            holder.tv_top.setText(Html.fromHtml("<font color='#CBCBC2'><big><big>"+i+"</big></big></font>"));
        }
        Glide.with(LiYueApplication.getInstance())
                .load(applyListBean.apply_user.avatar)
                .transform(new GlideCircleTransform(LiYueApplication.getInstance()))
                .error(R.drawable.default_error_image)
                .into(holder.iv_avater);
        holder.tv_username.setText(applyListBean.apply_user.nickname);
        holder.tv_named.setText(namedArr[position]);
        if (applyListBean.gift_list == null || applyListBean.gift_list.size() == 0) {
            holder.tv_gift_num.setText("霸王餐");
            holder.tv_gift_price.setText("￥0");
        } else {
            holder.tv_gift_num.setText("一共送给" + SexUtils.getXSex(appointmentDetailsResultBean.data.launch_user.sex) + applyListBean.gift_list.size() + "件礼物");
            holder.tv_gift_price.setText("￥" + applyListBean.total_cash);
        }
        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.e("applyListBean.apply_user.uid",applyListBean.apply_user.uid);
                onItemClickListener.onItemClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return appointmentDetailsResultBean.data.apply_list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_avater;
        TextView tv_top, tv_named, tv_username, tv_gift_num, tv_gift_price;
        RelativeLayout rl_item;

        public MyHolder(View itemView) {
            super(itemView);
            iv_avater = (ImageView) itemView.findViewById(R.id.iv_avater);
            tv_top = (TextView) itemView.findViewById(R.id.tv_top);
            tv_named = (TextView) itemView.findViewById(R.id.tv_named);
            tv_username = (TextView) itemView.findViewById(R.id.tv_username);
            tv_gift_num = (TextView) itemView.findViewById(R.id.tv_gift_num);
            tv_gift_price = (TextView) itemView.findViewById(R.id.tv_gift_price);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
        }

    }
}
